<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>
    <el-steps :active="2" process-status="wait" align-center style="margin:40px;
        bottom: 40px;"
    >
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>

    <el-button type="text" @click="openChapterDialog()">添加章节</el-button>

    <!-- 章节 -->
    <ul class="chapterList">
      <li
        v-for="chapter in chapterSectionList"
        :key="chapter.id"
      >
        <p>
          {{ chapter.title }}

          <span class="acts">
               <el-button style="" type="text" @click="openSectionDialog(chapter.id)">添加小节</el-button>
               <el-button style="" type="text" @click="editChapter(chapter.id)">编辑</el-button>
               <el-button type="text" @click="removeChapter(chapter.id)">删除</el-button>
           </span>
        </p>

        <!-- 小节 -->
        <ul class="chapterList sectionList">
          <li
            v-for="section in chapter.children"
            :key="section.id"
          >
            <p>{{ section.title }}
              <span class="acts">
               <el-button style="" type="text" @click="editSection(section.id)">编辑</el-button>
               <el-button type="text" @click="delSection(section.id)">删除</el-button>
           </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>
    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
    </div>

    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加和修改课时表单 -->
    <el-dialog :visible.sync="dialogSectionFormVisible" title="添加课时">
      <el-form :model="section" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="section.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="section.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="section.free">
            <el-radio :label="true">免费</el-radio>
            <el-radio :label="false">默认</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="上传视频">
          <el-upload
            :on-success="handleVodUploadSuccess"
            :on-remove="handleVodRemove"
            :before-remove="beforeVodRemove"
            :on-exceed="handleUploadExceed"
            :file-list="fileList"
            :action="'http://localhost:8000/admin/vod/video'"
            :limit="1"
            class="upload-demo"
          >
            <el-button size="small" type="primary">上传视频</el-button>
            <el-tooltip placement="right-end">
              <div slot="content">最大支持1G，<br>
                支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
                GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
                MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
                SWF、TS、VOB、WMV、WEBM 等视频格式上传
              </div>
              <i class="el-icon-question"/>
            </el-tooltip>
          </el-upload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSectionFormVisible = false">取 消</el-button>
        <el-button :disabled="saveSectionBtnDisabled" type="primary" @click="saveOrUpdateSection">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import chapter from '@/api/edu/chapter'
import section from '@/api/edu/section'

export default {
  data() {
    return {
      courseId: '', // 所属课程
      chapterSectionList: [], // 章节嵌套课时列表
      chapter: {},
      fileList: [
        name = ''
      ],
      section: {//封装小节数据
        title: '',
        sort: 0,
        isFree: 0,
        videoSourceId: '',//视频id
        videoOriginalName: ''//视频名称
      },
      dialogSectionFormVisible: false,//小节弹框
      saveBtnDisabled: false, // 保存按钮是否禁用
      dialogChapterFormVisible: false, // 章节弹框
      saveSectionBtnDisabled: false
    }
  },
  created() {
    //获取路由的id值
    if (this.$route.params && this.$route.params.id) {
      this.courseId = this.$route.params.id
      //根据课程id查询章节和小节
      this.getChapterAndSection()
    }
  },

  methods: {
    //上传视频超过最大数量的方法
    handleUploadExceed(file, fileList) {
      this.$message.warning('想要重新上传视频,请先删除已经上传的视频')
    },
    //点击确定调用的方法
    handleVodRemove() {
      section.removeAlyVideo(this.section.videoSourceId).then(response => {
        this.$message({
          type: 'success',
          message: '删除视频成功!'
        })
        //把文件列表清空
        this.fileList = []
        //删除的视频信息不再存入数据库
        this.section.videoSourceId = ''
        this.section.videoOriginalName = ''
      })

    },
    //点击×调用的方法
    beforeVodRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`)
    },
    //上传成功方法
    handleVodUploadSuccess(response, file, fileList) {
      this.section.videoSourceId = response.data
      this.section.videoOriginalName = file.name
    },
    //删除章节
    removeChapter(chapterId) {
      this.$confirm('此操作将永久删除章节记录,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {//点击确定
        chapter.delChapter(chapterId).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          //回到列表页面
          this.getChapterAndSection()
        }).catch((response) => {
          this.$message({
            type: 'error',
            message: response.data.message
          })
        })
      })
        .catch(() => { // 失败
            this.$message({
              type: 'info',
              message: '取消删除!'
            })
            this.getChapterAndSection()
          }
        )
    },
    //弹出添加章节表单
    openChapterDialog() {
      this.dialogChapterFormVisible = true
      this.chapter = { title: '', sort: 0 }
    },
    // 数据回显
    editChapter(id) {
      this.dialogChapterFormVisible = true
      chapter.getChapter(id).then(response => {
        this.chapter = response.data
      })
    },
    //编辑章节
    updateChapter() {
      chapter.updateChapter(this.chapter).then(response => {
        //给与提示
        this.$message({
          type: 'success',
          message: '章节编辑成功!'
        })
        //关闭弹框
        this.dialogChapterFormVisible = false
        this.getChapterAndSection()//查询所有章节/小节,刷新页面
      }).catch(response => {
        //给与提示
        this.$message({
          type: 'error',
          message: '章节编辑失败!'
        })
        //关闭弹框
        this.dialogChapterFormVisible = false
        this.getChapterAndSection()
      })

    },
    //增加章节
    addChapter() {
      this.chapter.courseId = this.courseId
      chapter.addChapter(this.chapter).then(response => {
        this.dialogChapterFormVisible = false
        //给与提示
        this.$message({
          type: 'success',
          message: '章节添加成功!'
        })
        //刷新页面
        this.getChapterAndSection()//这些公共语句即使类似也必须放在then里面,不然逻辑就会混乱...
      }).catch(response => {
        this.dialogChapterFormVisible = false
        //给与提示
        this.$message({
          type: 'error',
          message: '章节添加失败!'
        })
        //刷新页面
        this.getChapterAndSection()
      })
    },
    //增加和修改
    saveOrUpdate() {
      if (!this.chapter.id || this.chapter.id == '') {
        this.addChapter()
      } else {
        this.updateChapter()
      }
    },
    //根据课程id查询章节和小节
    getChapterAndSection() {
      chapter.getChapterAndSectionList(this.courseId).then((response) => {
        this.chapterSectionList = response.data
      })
    },

    //当点击更新/添加中的确认按钮时
    saveOrUpdateSection() {
      if (!this.section.id) {
        this.addSection()
      } else {
        this.updateSection()
      }
    },
    //当点击添加小节
    openSectionDialog(chapterId) {
      this.dialogSectionFormVisible = true
      this.chapterId = chapterId
      this.fileList = []
      this.section = {// 课时对象  //打开之前先清空
        title: '',
        sort: 0,
        isFree: 0,
        videoSourceId: '',
        videoOriginalName: ''
      }
    },
    //添加小节
    addSection() {
      this.section.courseId = this.courseId
      this.section.chapterId = this.chapterId
      section.addSection(this.section).then(response => {
        this.dialogSectionFormVisible = false
        //给与提示
        this.$message({
          type: 'success',
          message: '小节添加成功!'
        })
        //刷新页面
        this.getChapterAndSection()//这些公共语句即使类似也必须放在then里面,不然逻辑就会混乱...
      }).catch(response => {
        this.dialogSectionFormVisible = false
        //给与提示
        this.$message({
          type: 'error',
          message: '小节添加失败!'
        })
        //刷新页面
        this.getChapterAndSection()//这些公共语句即使类似也必须放在then里面,不然逻辑就会混乱...
      })
    },
    //编辑小节前的数据回显
    editSection(id) {
      this.openSectionDialog(id)//进行初始化
      this.dialogSectionFormVisible = true
      section.getSection(id).then(response => {
        //普通数据回显
        this.section = response.data
        //进行视频回显
        if (this.section.videoOriginalName != '') {
          this.fileList = [{ name: this.section.videoOriginalName }]
        }
      })
    },
    //编辑小节
    updateSection() {
      section.updateSection(this.section).then(response => {
        //给与提示
        this.$message({
          type: 'success',
          message: '小节编辑成功!'
        })
        //关闭弹框
        this.dialogSectionFormVisible = false
        this.getChapterAndSection()
      }).catch(response => {
        //给与提示
        this.$message({
          type: 'error',
          message: '小节编辑成功!'
        })
        //关闭弹框
        this.dialogSectionFormVisible = false
        this.getChapterAndSection()
      })
    },
    //删除小节
    delSection(id) {
      this.$confirm('此操作将永久删除小节记录,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {//点击确定
        section.delSection(id).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          //回到列表页面
          this.getChapterAndSection()
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除!'
        })
      })
    },

    previous() {
      console.log('previous')
      this.$router.push({ path: '/subject/info/' + this.courseId })
    },
    next() {
      console.log('next')
      this.$router.push({ path: '/subject/publish/' + this.courseId })
    }
  }
}
</script>
<style scoped>
.chapterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}

.chapterList li {
  position: relative;
}

.chapterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}

.chapterList .acts {
  float: right;
  font-size: 14px;
}

.sectionList {
  padding-left: 50px;
}

.sectionList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #DDD;
}</style>
